using UnityEngine;

public class UCameraControll : MonoBehaviour
{
    //滑动鼠标 相机旋转
    //鼠标滚轮 相机离角色远近 (Fov)

    [HideInInspector]
    public Transform target;

    [HideInInspector]
    public CharacterController characterController;

    [HideInInspector]
    public Vector3 hight_offset;
    void Start()
    {
        if (target != null)
        {
            Cursor.lockState = CursorLockMode.Locked;
            //Cursor.visible = false;

            //target = GameObject.Find("1001").transform;
            characterController  = target.GetComponent<CharacterController>();
            hight_offset  = characterController.center * 1.75f;

                       
        }
    }

    // Update is called once per frame

    float xMouse;
    float yMouse;

    float distanceFormTarget;

    [HideInInspector]
    public float mouse_scrollWheel_scale = 10; //缩放倍率

    [HideInInspector]
    public float speed  =5f;


    [HideInInspector]
    public int state; //0 空闲 1跟随

    public void SetTarget(Transform _target)
    {
        this.target = _target;
        if (target != null)
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;

            //target = GameObject.Find("1001").transform;
            characterController = target.GetComponent<CharacterController>();
            hight_offset = characterController.center * 1.75f;


        }
        state = 1;
        GameDefine._Camera.gameObject.SetActive(true);
        Follow();
    }
    private void LateUpdate()
    {
        Follow();
    }

    public void Follow(bool lerp = false)
    {
        if (state == 1 && target != null)
        {
            //鼠标滑动输入
            //-1 1 
            xMouse += UInput.GetAxis_Mouse_X();
            yMouse -= UInput.GetAxis_Mouse_Y();
            yMouse = Mathf.Clamp(yMouse, 10f, 30f);

            //滚轮滑动 往前是正 往后是负
            //需求是希望 滚轮向前 接近角色 滚轮向后 远离角色
            distanceFormTarget -= UInput.GetAxis_Mouse_ScrollWheel() * mouse_scrollWheel_scale;


            distanceFormTarget = Mathf.Clamp(distanceFormTarget, 2f, 5f);
            //Debug.Log("x:__"+x +"y:__" +y);


            Quaternion targetRotation = Quaternion.Euler(yMouse, xMouse, 0);
            Vector3 targetPosition = target.position + targetRotation * new Vector3(0, 0, -distanceFormTarget) + hight_offset; //在角色后面

            speed = characterController.velocity.magnitude > 0.1f ? Mathf.Lerp(speed, 7.5f, 5f * GameTime.deltaTime) : Mathf.Lerp(speed, 25f, 5f * GameTime.deltaTime);
            //角色控制器 如果处于控制状态 先慢到快的插值 缓动的目的


            //position 不想缓动了 
            transform.position = targetPosition; //Vector3.Lerp(transform.position, targetPosition, GameTime.deltaTime);
            transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, GameTime.deltaTime * 25);


            if (!lerp)
            {
                transform.position = targetPosition;
            }
            else
            {
                transform.position = Vector3.Lerp(transform.position, targetPosition, GameTime.deltaTime);
            }
        }
    }
}
